﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ggdll;
using System.Windows.Threading;


namespace Scoot
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private GG gg;
        private UI.Conversation.ConversationWindow conversationWindow;
        public MainWindow()                   
        {
            InitializeComponent();
            gg = new GG("41477737", "nnick2");
            //gg = new GG("13346677", "nnick2");
            gg.Login_Succesfull += new GG.GGSProtocolStatusEvent(gg_Login_Succesfull);
            gg.Login_Failed += new GG.GGSProtocolStatusEvent(gg_Login_Failed);
            gg.List_Updated += new GG.GGContactListEvent(gg_ListUpdated);

            gg.Status_Changed += new GG.GGUserStatusEventHandler(gg_Status_Changed);
            gg.Status_Updates += new GG.GGContactListStatusEvent(gg_Status_Updates);

            gg.Message_Arrived += new GG.GGIncomingMessageEventHandler(gg_Message_Arrived);

            conversationWindow = new UI.Conversation.ConversationWindow();

            conversationWindow.conversationControl.Send_Message += new UI.Conversation.ConversationControl.SendMessageHandler(conversationControl_Send_Message);
        }

        void conversationControl_Send_Message(int uin, string message)
        {
            gg.sendMessage(uin, message);
        }


        void gg_Message_Arrived(ggdll.GGMessages.Incoming.GGIncomingMessage msg)
        {
            Dispatcher.Invoke(DispatcherPriority.Background,
                                  new Action(() =>
                                  {
                                      conversationWindow.addMessage(msg.plain_message, msg.sender);
                                  }));
        }

        void gg_Status_Updates(List<ggdll.GGContacts.GGContactState> states)
        {
            Dispatcher.Invoke(DispatcherPriority.Background,
                                  new Action(() =>
                                  {
                                      foreach (ggdll.GGContacts.GGContactState it in states)
                                      {
                                          foreach (UI.ContactTile it2 in ContactList.Items)
                                          {
                                              if (it.uin == it2.uin)
                                              {
                                                  it2.changeState(it);
                                              }
                                          }
                                      }
                                  }));
           
        }

        void gg_Status_Changed(ggdll.GGContacts.GGContactStatusEnum state)
        {
            statusBox.changeIcon(state);
        }

        void gg_ListUpdated(System.Collections.Generic.List<ggdll.GGContacts.GGGroup> groups, System.Collections.Generic.List<ggdll.GGContacts.GGContact> contacts)
        {
            //MessageBox.Show("!");
            Dispatcher.Invoke(DispatcherPriority.Background,
                                  new Action(() =>
                                  {
                                      ContactList.Items.Clear();
                                      //MessageBox.Show("!!");
                                      foreach (ggdll.GGContacts.GGContact contact in contacts)
                                      {
                                          UI.ContactTile ct = new UI.ContactTile(contact.ShowName, contact.Uin);
                                          ct.Double_Click += new UI.ContactTile.ContactTileEventHandler(ct_Double_Click);
                                          ContactList.Items.Add(ct);
                                      }
                                  }));
        }


        void ct_Double_Click(UI.ContactTile sender)
        {
            conversationWindow.openTab(sender.uin);
        }

        void gg_Login_Failed()
        {
            throw new NotImplementedException();
        }

        void gg_Login_Succesfull()
        {
            Dispatcher.Invoke(DispatcherPriority.Background,
                                  new Action(() =>
                                  {
                                     UserUIN.Text = gg._uin;
                                  }));

            if (ContactList.Items.Count == 0)
            {
                gg.notifyEmptyContactList();
                gg.requestContactList();
            }
            else gg.requestContactList();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            conversationWindow.Dispose();
            gg.Dispose();
        }
        

        private void statusBox_StatusChanged(ggdll.GGContacts.GGContactStatusEnum state, string description)
        {
            gg.requestStatusChange(state, description);
        }






    }
}
